package main

// 使用json包生成json格式的数据并写入文件

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
)

type Address struct {
	Type    string
	City    string
	Country string
}

type VCard struct {
	Name      string
	Addresses []*Address
	Remark    string
}

func main() {
	pa := &Address{"private", "Beijing", "China"}
	wk := &Address{"work", "Wuhan", "China"}
	vc := VCard{"Hanmeimei", []*Address{pa, wk}, "none"}

	jc, _ := json.Marshal(vc)
	fmt.Printf("json formated is: %s\n", jc)

	file, _ := os.OpenFile("vcard.json", os.O_WRONLY|os.O_CREATE, 0666)
	enc := json.NewEncoder(file)
	if err := enc.Encode(vc); err != nil {
		log.Printf("Encoding Josn Error: %s", err.Error())
	}
}
